<template>
  <div class="main-container">
    <div class="menu game-menu" v-show="!showGame && !showConfig">
      <h2>mine sweeper</h2>
      <button @click="launchGame" style="margin-right: 20px;">Play Game</button>
      <button @click="openSettings">Game Settings</button>
    </div>
    <div class="menu settings-menu" v-show="showConfig">
      <h1>Game Settings</h1>
      <VeeForm v-slot="{ errors }">
        <div class="settings-label">
          <span>rows: </span>
        </div>
        <Field 
          name='rows' 
          :rules="rowRule" 
          v-model.number="config.rows"
          class="half"
        ></Field>
        <ErrorMessage name='rows'></ErrorMessage>
        <div class="settings-label">
          <span>cols: </span>
        </div>
        <Field 
          name='cols' 
          :rules="colRule" 
          v-model.number="config.cols"
          class="half"
        ></Field>
        <ErrorMessage name='cols'></ErrorMessage>
        <div class="settings-label">
          <span>mines: </span>
        </div>
        <Field 
          name='mines' 
          :rules="mineRule" 
          v-model.number="config.mines"
          class="half"
        ></Field>
        <ErrorMessage name='mines'></ErrorMessage>
        <div>
          <button :disabled="Object.keys(errors).length" @click.prevent="saveSettings">go back</button>
        </div>
      </VeeForm>
    </div>
    <div class="game game-board" v-show="showGame">
          <Board
            ref="gb"
          >
            <template v-slot="{ gameStatus, gameResult }">
              <div class="gb-overlay" v-show="gameStatus===2">
                <h2>You {{gameResult ? 'Win' : 'Lose'}}!</h2>
                <button @click="launchGame">Play Again</button>
              </div>
            </template>
          </Board>
          <button @click="goTitle">back to title</button>
    </div>
  </div>
</template>

<script>
import Board from './components/board.vue'
import { Field, Form as VeeForm, ErrorMessage } from 'vee-validate'
import { computed, defineComponent, nextTick, reactive, ref } from 'vue'

export default defineComponent({
  components: {
    Board,
    Field,
    ErrorMessage,
    VeeForm
  },
  setup() {
    const showGame = ref(false)
    const showConfig = ref(false)
    const config = reactive({
      rows: 9,
      cols: 9,
      mines: 5
    })

    const mineLimit = computed(() => config.rows * config.cols)

    const gb = ref(null)

    const launchGame = () => {
      gb.value.resetBoard()
      nextTick(() => {
        gb.value.generateBoard(config)
        showGame.value = true
      })
    }

    const openSettings = () => {
      showConfig.value = true
    }

    const saveSettings = () => {
      showConfig.value = false
    }

    const goTitle = () => {
      gb.value.resetBoard()
      showGame.value = false
    }

    const rowRule = (val) => {
      if(!val){
        return 'must have row value'
      }
      if(val < 2) {
        return 'at least 2 rows'
      }
      return true
    }

    const colRule = (val) => {
      if(!val){
        return 'must have col value'
      }
      if(val < 2) {
        return 'at least 2 cols'
      }
      return true
    }

    const mineRule = ((mineLimit) =>(val) => {
      if(!val) {
        return 'must set mines'
      }
      if(val < 1 ) {
        return 'at least 1 mine'
      }
      if(val > mineLimit.value - 1) {
        return `at most ${mineLimit.value - 1} mines`
      }
      return true
    })(mineLimit)

    return {
      showGame,
      showConfig,
      config,
      gb,
      launchGame,
      openSettings,
      saveSettings,
      goTitle,
      rowRule,
      colRule,
      mineRule
    }
  }
})
</script>

<style scoped>
.main-container{
  display: flex;
  justify-content: center;
  align-items: center;
  margin-top: 100px;
}
</style>
